package com.cszy.yydqbfq.ui.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.cszy.yydqbfq.R
import com.cszy.yydqbfq.databinding.ActivityGeDanListBinding
import com.cszy.yydqbfq.adapter.GeDanDetailListAdapter
import com.cszy.yydqbfq.base.BaseVMActivity
import com.cszy.yydqbfq.bean.SongListBean
import com.cszy.yydqbfq.dialog.BottomCreateSongPop
import com.cszy.yydqbfq.ext.clickWithTrigger
import com.cszy.yydqbfq.ext.toast
import com.cszy.yydqbfq.listener.OnCreateSongListener
import com.cszy.yydqbfq.viewmodel.SongListViewModel
import com.gyf.immersionbar.ktx.immersionBar
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener
import com.hnyyac.ad.mds.utils.BaseAdConstants

class GeDanListActivity : BaseVMActivity<SongListViewModel, ActivityGeDanListBinding>(),
    IAdViewListener{
    private val songList: List<SongListBean>? = null
    private var madapter: GeDanDetailListAdapter? = null
    private var songlistbeanId: Long? = null
    private var newGromoreFeedAd: NewGroMoreFeedAdManager? = null
    var type_proces: String? = null //功能入口类型


    override fun initView(savedInstanceState: Bundle?) {


    }

    override fun initData() {
        type_proces = "第一次插屏"
        AdManager.loadChapin_nocache(
            this,
            55,
            this,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {
                    newGromoreFeedAd = NewGroMoreFeedAdManager(
                        this@GeDanListActivity,
                        BaseAdConstants.FEEDAD_Id,
                        mBinding?.frameLayoutBanner,
                        22
                    )
                    newGromoreFeedAd?.loadAd()
                }
            })

        songlistbeanId = intent?.extras?.getLong("id")
        val name = intent?.extras?.getString("name")
        mViewModel.loadPlaylistDetail(songlistbeanId!!)
        madapter = GeDanDetailListAdapter()
        mBinding.headTitleRoot.root.setBackgroundColor(
            ContextCompat.getColor(
                this,
                R.color.statubar_color
            )
        )
        immersionBar {
            statusBarColor(R.color.statubar_color)
            fitsSystemWindows(false)
        }
        mBinding.headTitleRoot.headBack.setImageResource(R.mipmap.ic_white_back)
        mBinding.headTitleRoot.headCenterTitle.text = name
        mBinding.headTitleRoot.headCenterTitle.setTextColor(ContextCompat.getColor(this, R.color.white))
        mBinding.recySongList.layoutManager = LinearLayoutManager(this)
        mBinding.recySongList.adapter = madapter
        val emptyView =
            layoutInflater.inflate(R.layout.layout_empty_view, mBinding.recySongList, false)
        madapter?.isEmptyViewEnable = true
        madapter?.emptyView = emptyView

    }

    override fun initObserve() {
        mViewModel.currentPlaylist.observe(this) {
            madapter?.submitList(it?.songs)
        }
    }

    override fun initListener() {
        super.initListener()
        mBinding.headTitleRoot.headBack.setOnClickListener {
            finish()
        }

        madapter?.addOnItemChildClickListener(R.id.tv_remove_gedanlist) { adapter, view, position ->
            toast("移除成功")
            mViewModel.removeSongFromPlaylist(adapter.items[position].id, songlistbeanId!!)

        }
        //点击进详情播放
        madapter?.setOnItemClickListener { adapter, view, position ->
            // 传递整个列表和当前点击位置
            var songList = mViewModel.convertToSongInfoList(adapter.items)
            val intent = Intent(this, PlayDetailActivity::class.java).apply {
                putParcelableArrayListExtra("MUSIC_LIST", ArrayList(songList))
                putExtra("CURRENT_POSITION", position)
            }
            startActivity(intent)
        }
    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {
        when (type_proces) {
            "第一次插屏" -> {
                newGromoreFeedAd = NewGroMoreFeedAdManager(
                    this,
                    BaseAdConstants.FEEDAD_Id,
                    mBinding?.frameLayoutBanner,
                    22
                )
                newGromoreFeedAd?.loadAd()
            }
        }
    }

    override fun onError(msg: String) {

    }

    override fun onLoadError(adView: IAdView) {

    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {

    }
}